package cc.comac.mom.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairStepService;
import cc.comac.mom.bean.EquipDev;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.bean.EquipRepairStep;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipDevDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositoryRepairStepDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairStepDataHandler;
import cc.comac.mom.datahandlers.entity.QualityAttachmentGroupDataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
@Transactional
public class EquipRepairStepServiceImpl implements EquipRepairStepService {

	@Autowired
	private EquipRepairStepDataHandler equipRepairStepDataHandler;

	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;

	@Autowired
	private EquipDevDataHandler equipDevDataHandler;
	
	@Resource
	private IdFactory idFactory;
	
	@Autowired
	private EquipRepairRepositoryRepairStepDataHandler equipRepairRepositoryRepairStepDataHandler;

	@Override
	public ResultBean insertBatch(@RequestBody List<EquipRepairStep> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualityAttachmentGroup> listQualityAttachmentGroup = new ArrayList<QualityAttachmentGroup>();
		for (EquipRepairStep item : list) {
			List<QualityAttachmentGroup> listAttachmentGroup = item.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long id = idFactory.generateId();
				item.getInnerData().setAttachmentGroupId(id);
				for (QualityAttachmentGroup item2 : listAttachmentGroup) {
					item2.getInnerData().setGroupId(id);
				}
				listQualityAttachmentGroup.addAll(listAttachmentGroup);
			}
		}
		if(listQualityAttachmentGroup!= null && listQualityAttachmentGroup.size()>0) {
			ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listQualityAttachmentGroup);
			if (!resultBean2.success) {
				throw new RuntimeException("保存附件组信息失败");
			}
		}
		ResultBean resultBean3 = equipRepairStepDataHandler.insertBatch(list);
		if (!resultBean3.success) {
			throw new RuntimeException(resultBean3.getMessage());
		}
		return resultBean;
	}

	@Override
	public ResultBean update(@RequestBody EquipRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long attachmentGroupId = obj.getInnerData().getAttachmentGroupId();
				if (attachmentGroupId == 0) {
					long id = idFactory.generateId();
					obj.getInnerData().setAttachmentGroupId(id);
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(id);
					}
				} else {
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(attachmentGroupId);
					}
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}

		}
		ResultBean resultBean2 = equipRepairStepDataHandler.update(obj);
		if (!resultBean2.success) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteBatch(@RequestBody List<EquipRepairStep> list) {
		// TODO Auto-generated method stub
		return equipRepairStepDataHandler.deleteBatch(list);
	}

	@Override
	public ResultBean moveUp(@RequestBody EquipRepairStep obj) {
		// TODO Auto-generated method stub
		return equipRepairStepDataHandler.moveUp(obj);
	}

	@Override
	public ResultBean moveDown(@RequestBody EquipRepairStep obj) {
		// TODO Auto-generated method stub
		return equipRepairStepDataHandler.moveDown(obj);
	}

	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectByCondition = equipRepairStepDataHandler.selectByCondition(obj);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public ResultBean getRepqirRepositoryStepInfo(@RequestBody EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			return null;
		}
		long faultCauseId = obj.getInnerData().getFaultCauseId();
		if (faultCauseId == 0) {
			resultBean.setErrorMessge("请先进行维修分析");
			return resultBean;
		}
		EquipDev equipDev = equipDevDataHandler.getById(obj.getInnerData().getDevId());
		if (equipDev == null) {
			resultBean.setErrorMessge("无法获取设备主数据信息");
			return resultBean;
		}
		EquipRepairRepository equipRepairRepository = new EquipRepairRepository();
		EquipRepairRepositoryEntity equipRepairRepositoryEntity = new EquipRepairRepositoryEntity();
		equipRepairRepository.setPageNum(obj.getPageNum());
		equipRepairRepository.setPageSize(obj.getPageSize());
		equipRepairRepository.setRepairItem(obj.getRepairItem());
		equipRepairRepository.setRepairContent(obj.getRepairContent());
		equipRepairRepositoryEntity.setEquipCategoryId(equipDev.getInnerData().getDevCategoryId());
		equipRepairRepositoryEntity.setFaultCauseId(obj.getInnerData().getFaultCauseId());
		equipRepairRepository.setInnerData(equipRepairRepositoryEntity);
		ResultDev selectByCondition = equipRepairRepositoryRepairStepDataHandler.selectByCondition(equipRepairRepository);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

}
